package Client;

import model.*;
import util.ClientScanner;
import util.GetRandom;
import util.Io;

import java.io.IOException;
import java.util.ArrayList;


/**
 * 编程实现客户端主页面的绘制和相应功能的实现
 */
public class ClientView {
    /**
     * 使用合成复用原则
     */
    private ClientInitClose cic;

    public ClientView(ClientInitClose cic){
        this.cic = cic;
    }
    // 声明一个用来存放成绩的集合
    ArrayList<StudentScore> list = new ArrayList<>();

    /**
     * 自定义成员方法实现客户端主页面的绘制
     */
    public void clinetMainPage() throws IOException, ClassNotFoundException, InterruptedException {

        while(true){
            System.out.println("\n\n\t\t 在线考试系统");
            System.out.println("----------------------------");
            System.out.print("   [1] 学员登录");
            System.out.println("   [2] 管理员登录");
            System.out.println("   [0] 退出系统");
            System.out.println("----------------------------");
            System.out.println("请输入你要操作的业务");

            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入学员系统。。。");
                    clinetUserLogin();
                    //StudentClinetView.studentClinetMainPage(cic);
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("退出系统");
                    // todo 传递一个退出系统的参数给服务端
                    UserMassage massage = new UserMassage("endExam");
                    // 将UserMassage对象通过输出流发送给服务器
                    cic.getOos().writeObject(massage);
                    return;
                default:
                    System.out.println("输入错误,请重新输入！");
            }
        }
    }

    /**
     * 学员登录功能的实现
     */
    private void clinetUserLogin() throws IOException, ClassNotFoundException, InterruptedException {
        System.out.println("请输入学员的账号");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入学员的密码");
        String password = ClientScanner.getScanner().next();
        User user = new User(name,password);
        cic.getOos().reset();
        cic.getOos().writeObject(new StudentMessage("userCheck",user));
        System.out.println("客户端发送信息成功");
        // 接收服务器返回的消息
        StudentMessage studentMessage = (StudentMessage) cic.getOis().readObject();
        if("success".equalsIgnoreCase(studentMessage.getType())){
            // 学生登录成功
            Student student = studentMessage.getStudent();
            userModuleChoose(student);

            System.out.println("学员登录成功");
        }else{
            System.out.println("账号密码错误");
        }

    }

    /**
     * 用户模块选择
     * @param student
     */
    private void userModuleChoose(Student student) throws IOException, ClassNotFoundException, InterruptedException {
        while (true){
            System.out.println("                用户模块选择页面");
            System.out.println("——————————————————————————————————————————");
            System.out.print("[1] 用户模块      ");
            System.out.println("[2] 考试模块");
            System.out.println("[0] 退出");
            System.out.println("————————————————————————————————————————");
            System.out.println("请输入想要进行的业务号");
            // 从库里读取考试成绩
            list = Io.readScore();

            //获取扫描器对象
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    userModuleView(student);break;
                case 2:
                    userExamModule(student);break;
                case 0:
                    clinetMainPage();break;
                default:
                    System.out.println("输入有误，重新输入");
            }
        }
    }


    /**
     * 用户考试模块
     * @param student
     */
    private void userExamModule(Student student) throws InterruptedException, IOException, ClassNotFoundException {
        while(true){
            System.out.println("                考试模块");
            System.out.println("————————————————————————————————————————");
            System.out.print("[1] 开始考试      ");
            System.out.println("[2] 导出成绩");
            System.out.print("[3] 查询成绩   ");
            System.out.println("[0] 退出");
            System.out.println("————————————————————————————————————————");
            System.out.println("请输入想要进行的业务号");
            //获取扫描器对象
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    // 开始考试
                    startExam(student);break;
                case 2:
                    // 导出成绩
                    saveScore(student);break;
                case 3:
                    // 查询成绩
                    searchScore(student);break;
                case 0:
                    userModuleChoose(student);break;
                default:
                    System.out.println("输入有误，重新输入");
            }
        }
    }

    /**
     * 查询成绩
     * @param student
     */
    private void searchScore(Student student) throws IOException, ClassNotFoundException {
        while(true){
            StudentMessage studentMessage = new StudentMessage("findScore",student);
            cic.getOos().reset();
            cic.getOos().writeObject(studentMessage);
            studentMessage = (StudentMessage) cic.getOis().readObject();
            if (studentMessage.getType().equals("查询成绩")){
                System.out.println("查询分数成功");
                return;
            }else{
                System.out.println("查询分数失败");
                return;
            }

        }
    }

    /**
     * 导出成绩功能
     * @param student
     */
    private void
    saveScore(Student student) throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("是否导出"+student.getName()+"的分数？yes/no");
            String next = ClientScanner.getScanner().next();
            if("yes".equals(next)){
                StudentMessage studentMessage = new StudentMessage("saveScore",student);
                cic.getOos().reset();
                cic.getOos().writeObject(studentMessage);
                studentMessage = (StudentMessage) cic.getOis().readObject();
                if (studentMessage.getType().equals("导出成绩")){
                    System.out.println("导出成功");
                    return;
                }else{
                    System.out.println("导出分数失败");
                    return;
                }
            }else if("no".equalsIgnoreCase(next)){
                /*userExamModule(student);*/
                return;
            }
        }
    }

    /**
     * 开始考试功能
     * @param student
     */
    private void startExam(Student student) throws IOException, ClassNotFoundException {
        while(true){
            System.out.println("正在进入考试...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 定义开始的分数
            int score = 0;
            StudentScoreMessage studentScoreMessage = new StudentScoreMessage("startExam",new StudentScore(student,score));
            // 把消息发送给服务端,让服务端发送考题
            try {
                cic.getOos().reset();
                cic.getOos().writeObject(studentScoreMessage);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("等待发卷...");
            ArrayList<Topic> topics = (ArrayList<Topic>) cic.getOis().readObject();
            // 产生随机题号中。。。
            int [] titles = GetRandom.rdArr();
            System.out.println("产生的题号有:"+titles);
            // 因为就只有5道题
            for(int i=0;i<5;i++){
                System.out.println("题号为:"+titles[i]);
                // 打印考题
                System.out.println(topics.get(titles[i]));
                System.out.println("请回答...");
                String answer = ClientScanner.getScanner().next();
                if (answer.equals(topics.get(i).getModelAnswer())){
                    score += 20;
                    System.out.println("回答正确,+20分");
                }else{
                    System.out.println("回答错误,没分");
                }

            }
            studentScoreMessage = new StudentScoreMessage("endExam",new StudentScore(student,score));
            // 把成绩往库里存
            list.add(new StudentScore(student,score));
            Io.saveSore(list);
            cic.getOos().reset();
            cic.getOos().writeObject(studentScoreMessage);//将结束的分数上传
            System.out.println("考试结束，上传信息成功");
            return;

        }
    }

    /**
     * 用户模块
     * @param student
     */
    private void userModuleView(Student student) throws IOException, ClassNotFoundException, InterruptedException {
        while(true){
            System.out.println("                用户模块");
            System.out.println("————————————————————————————————————————");
            System.out.print("[1] 修改密码      ");
            System.out.println("[0] 退出");
            System.out.println("————————————————————————————————————————");
            System.out.println("请输入想要进行的业务号");
            //获取扫描器对象
            int choose = ClientScanner.getScanner().nextInt();
            switch(choose){
                case 1:
                    updatePassword(student);break;
                case 0:
                    userModuleChoose(student);break;
                default:
                    System.out.println("输入有误，重新输入");
            }
        }
    }

    /**
     * 普通用户修改密码
     * @param student
     */
    private void updatePassword(Student student) throws IOException, ClassNotFoundException {
            while(true){
                System.out.println("请输入原始密码");
                // 输入的原始密码确保跟原来的密码一致
                String oldPassword = ClientScanner.getScanner().next();
                // 从对象里边拿到的新密码
                String password = student.getUser().getPassword();
                // 比对
                if (password.equals(oldPassword)){
                    System.out.println("输入新的密码");
                    String newPassword = ClientScanner.getScanner().next();
                    // 将新密码放入，替换掉原来的密码
                    student.getUser().setPassword(newPassword);
                    StudentMessage studentMessage = new StudentMessage("updatePassword",student);
                    cic.getOos().reset();
                    cic.getOos().writeObject(studentMessage);
                    // System.out.println("更新密码成功");
                    // 从服务端读消息
                    studentMessage = (StudentMessage) cic.getOis().readObject();
                    if(studentMessage.getType().equalsIgnoreCase("success")){
                        System.out.println("密码修改成功");
                        return;
                    }else{
                        System.out.println("密码修改失败");
                    }

                }
            }

    }

    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入用户名:");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入密码:");
        String password = ClientScanner.getScanner().next();
        UserMassage massage = new UserMassage("managerCheck", new User(userName, password));
        cic.getOos().reset();
        // 将UserMassage对象通过输出流发送给服务器
        cic.getOos().writeObject(massage);
        System.out.println("客户端输入完毕!");
        // 接收服务器的消息并给出提示
        UserMassage massage1 = (UserMassage) cic.getOis().readObject();
        if("success".equals(massage1.getType())){
            System.out.println("登录成功!");

        }else{
            System.out.println("用户名或密码错误!");
        }
    }
}
